package com.glela.order.service.impl.order;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import com.glela.common.util.JsonUtil;
import com.glela.order.model.OrderObject;
import com.glela.order.model.OrderSettlementCommision;
import com.glela.order.service.OrderService;
import com.glela.platform.model.response.ResponseUser;
import com.glela.platform.service.PromotionService;
import com.glela.platform.service.UserService;

/**
 * **********************************************
 * @Title: 开店送礼包监听队列
 * @throws @author gaoweiqi
 * @date 2017年01月12日 下午5:21:27 
 * **********************************************
 */
public class AmqpOpenStorePromotion implements MessageListener{ 
    private static Logger logger = LoggerFactory.getLogger(AmqpOpenStorePromotion.class);

	private PromotionService  promotionService;
	private UserService	userService;
	private OrderService	orderService2;

	public void onMessage(Message message) {
		OrderSettlementCommision temp=null;
		String orderThing = null;
		try {
			//转换消息实体
			orderThing = new String(message.getBody());
			temp = JsonUtil.toBean(orderThing, OrderSettlementCommision.class);
		} catch (Exception e) {
			logger.error("开店送优惠券队列接收消息错误：{}-{}-{}", orderThing,message.getBody(), e.getMessage());
		}
		try {
			OrderObject objorder=orderService2.selectOrderObjectByOrderSn(temp.getOrderSn());
			if(objorder!=null&&objorder.getOpenStore()==1){//开店单送
				ResponseUser info=
				userService.selectUserRoleById(objorder.getUserId());
				promotionService.addOpenStorePromotionPackages(temp.getOrderSn(), info.getUserId(), info.getRoleId());
			}
		} catch (Exception e) {
			logger.error("开店送优惠券队列处理失败：{}-{}", orderThing, e.getMessage());
		}
	}
	public void setPromotionService(PromotionService promotionService) {
		this.promotionService = promotionService;
	}
 
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setOrderService2(OrderService orderService2) {
		this.orderService2 = orderService2;
	}
}
